package com.party.admin.biz.sign;

import com.party.admin.web.dto.input.common.CommonQuery;
import com.party.admin.web.dto.input.sign.SignGroupInput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.sign.GroupAuthor;
import com.party.core.model.sign.SignApply;
import com.party.core.model.sign.SignGroup;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignGroupService;
import com.party.core.service.sign.biz.SignBusinessBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 签到小组业务接口
 * Created by wei.li
 *
 * @date 2017/6/7 0007
 * @time 11:42
 */
@Service
public class SignGroupBizService {

    @Autowired
    private ISignGroupService signGroupService;

    @Autowired
    private ISignApplyService signApplyService;

    @Autowired
    private SignBusinessBizService signBusinessBizService;


    /**
     * 获取签到分组
     *
     * @param input 签到分组
     * @param page 分页
     * @return 分组列表
     */
    public List<GroupAuthor> list(SignGroupInput input, Page page){
        String startTime = input.getStartDate();
        String endTime = input.getEndDate();
        if (null != input.getTimeType() && !new Integer(0).equals(input.getTimeType())) {
            CommonQuery commonQuery = new CommonQuery();
            commonQuery.setTimeType(input.getTimeType());
            startTime = commonQuery.getCreateStart();
            endTime = commonQuery.getCreateEnd();
        }

        GroupAuthor groupAuthor = SignGroupInput.transform(input);
        groupAuthor.setStartDate(startTime);
        groupAuthor.setEndDate(endTime);
        List<GroupAuthor> list = signGroupService.groupAuthorList(groupAuthor, page);
        return list;
    }

    /**
     * 小组排行
     *
     * @param groupAuthor 查询条件
     * @param type        （默认：全部， 0：今天，1：本周，2：本月，3：一月前）
     * @param page        分页参数
     * @return 排行列表
     */
    public List<GroupAuthor> rankListXcx(GroupAuthor groupAuthor, Integer type, Page page) {
        Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
        params.put("name", groupAuthor.getName());
        List<GroupAuthor> list = signGroupService.rankListXcx(groupAuthor.getProjectId(), params, page);
        return list;
    }

    /**
     * 取消队伍
     * @param applyId 报名id
     */
    @Transactional
    public void cancelJoinGroup(String applyId) {
        SignApply signApply = signApplyService.get(applyId);
        String oldGroupId = signApply.getGroupId();
        Long stepNum = signApply.getStepNum();
        signApply.setGroupId("");
        signApplyService.update(signApply);

        if (StringUtils.isNotEmpty(oldGroupId)) {
            SignGroup signGroup = signGroupService.get(oldGroupId);
            signGroup.setApplyNum(signGroup.getApplyNum() - 1);
            signGroup.setPassNum(signGroup.getPassNum() - 1);
            signGroup.setMemberNum(signGroup.getMemberNum() - 1);
            // 步数计算
            signGroup.setStepNum(signGroup.getStepNum() - stepNum);
            signGroupService.update(signGroup);
        }
    }
}
